﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DDE_Server.View;
using DDE_Server.Object;
using DDE_Server.Common;
using DDE_Server.Event;

namespace DDE_Server.Presenter
{
    
    public class StrategyPresenter
    {
        private event DataChangedEventHandler dataChange = null;
        private bool resultChange = false;
        private bool outTimeChange = false;
        private System.Windows.Forms.Timer countDown = null;
        private StrategyView view=null;
		PjmInstDispRate instDisRate = null;
        public StrategyPresenter(StrategyView view)
        {
            this.view = view;
            countDown = new System.Windows.Forms.Timer();
            countDown.Interval = 1000;
            countDown.Tick += (CountDownTick);
            countDown.Start();
        }
        public void Refresh()
        {
            view.Refresh();
        }
        public event DataChangedEventHandler DataChange
        {
             add {
                if (dataChange == null || !dataChange.GetInvocationList().Contains(value)) {
                    dataChange += value;
                }
            }
            remove {
                dataChange -= value;
            }
        }
        public void UpdateResult(PjmInstDispRate disRate)
        {
			instDisRate = disRate;
            resultChange = false;
            for (int i = 0; i < view.ListData.Count; i++) {
                UpdateResultEntry(i, disRate);
            }
			view.ResetBinding();
            if(resultChange){
                dataChange(this, new DataChangedEventArgs(view.ListData));
            }


        }
        public void UpdateResultEntry(int index,PjmInstDispRate disRate) 
        {
            if (view.ListData[index].status == Result.FALSE && view.ListData[index].timer >  new TimeSpan(0, 0, 0))// NONE
            {
                switch (view.ListData[index].condition)
                {
                    case Compare.G:
                        if (disRate.GetDistpatch(view.ListData[index].distpatch) > view.ListData[index].priceConditional)
                        {
                            view.ListData[index].status = Result.TRUE;
                            resultChange = true;
                        }
                        break;
                    case Compare.L:
                        if (disRate.GetDistpatch(view.ListData[index].distpatch) < view.ListData[index].priceConditional)
                        {
                            view.ListData[index].status = Result.TRUE;
                            resultChange = true;
                        }
                        break;
                }
            }
        }
        private void UpdateCountDown(List<DataEntry> list)
        {
            outTimeChange = false;
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].status == Result.FALSE)
                    {
                        if (list[i].timer > new TimeSpan(0, 0, 0))
                        {
                            list[i].timer = list[i].timer.Subtract(TimeSpan.FromSeconds(1));
                        }
                        else
                        {
                            //list[i].status = Result.FALSE;
                            //outTimeChange = true;
                        }
                    }

                }
                view.Refresh();
           // if(outTimeChange){
           //     dataChange(this, new DataChangedEventArgs(view.ListData));
           // }
        }
        private void CountDownTick(object sender, EventArgs e)
        {
            countDown.Stop();
            List<DataEntry> listData = view.ListData;
            UpdateCountDown(listData);
            countDown.Start();

        }

        public void AddRow(DataEntry ent)
        {
            view.ListData.Add(ent);
			UpdateResultEntry(view.ListData.Count - 1, instDisRate);
            view.ResetBinding();
            dataChange(this, new DataChangedEventArgs(view.ListData));
        }

        public void RemoveRow(int index)
        {
            view.ListData.RemoveAt(index);
            dataChange(this, new DataChangedEventArgs(view.ListData));
        }

        public void ResetTimer(int index) 
        {
            if ((index >=0 ) && (index < view.ListData.Count))
            {
                view.ListData[index].timer = view.ListData[index].timerCount;
                view.ListData[index].status = Result.FALSE;
				UpdateResultEntry(index, instDisRate);
                dataChange(this, new DataChangedEventArgs(view.ListData));
            }
            view.Refresh();
        }

        public void StopRow(int index)
        {
            //for (int i = 0; i < view.ListData.Count; i++) 
            if ((index >= 0) && (index < view.ListData.Count))
            {
                view.ListData[index].status = Result.FALSE;
                view.ListData[index].timer = new TimeSpan(0, 0, 0);
                dataChange(this, new DataChangedEventArgs(view.ListData));
            }
            
            view.Refresh();
        }
    }
}
